﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphBase.Interfaces;
using ObjectGraph.Core.Interfaces;

namespace GraphBase
{
    public class Graph : GraphObject, IGraph
    {
        protected VertexCollection m_vertexes;
        protected EdgeCollection m_edges;

        public event OnGraphAction EdgeAddedEvent;
        public event OnGraphAction EdgeRemovedEvent;
        public event OnGraphAction VertexAddedEvent;
        public event OnGraphAction VertexRemovedEvent;

        public IVertexCollection Vertexes { get { return m_vertexes; } }
        public IEdgeCollection Edges { get { return m_edges; } }

        public Graph()
        {
            VertexCollection vertexes = new VertexCollection( OnVertexAdded, OnVertexRemoved );
            EdgeCollection edges = new EdgeCollection(vertexes.GetVertexEnumerator, OnEdgeAdded, OnEdgeRemoved);
            Construct(vertexes, edges);
        }

        public Graph(VertexCollection vertexes, EdgeCollection edges)
        {
            Construct(vertexes, edges);
        }

        private void Construct(VertexCollection vertexes, EdgeCollection edges)
        {
            m_vertexes = vertexes;
            m_edges = edges;
            zzSetGraph(this);
        }

        protected virtual void OnEdgeAdded(Edge edge, Vertex from, Vertex to )
        {
            if (EdgeAddedEvent != null)
                EdgeAddedEvent(new EdgeAction( this, edge, from, to, ECollectionActionType.Add) );
        }

        protected virtual void OnEdgeRemoved(Edge edge, Vertex from, Vertex to)
        {
            if (EdgeRemovedEvent != null)
                EdgeRemovedEvent(new EdgeAction( this, edge, from, to, ECollectionActionType.Remove) );
        }

        protected virtual void OnVertexAdded(Vertex vertex)
        {
            if (VertexAddedEvent != null)
                VertexAddedEvent(new VertexAction( this, vertex, ECollectionActionType.Add ));
        }

        protected virtual void OnVertexRemoved(Vertex vertex)
        {
            if (VertexRemovedEvent != null)
                VertexRemovedEvent(new VertexAction( this, vertex, ECollectionActionType.Remove ));
        }
    }
}
